﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;

namespace Ewts.RDLC
{
    public partial class CjoReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int cjoID = 0;
                DataLib.Model.CJOItem cjoIteM = null;
                if (Request["cjoID"] != null && Request["cjoID"].ToString().Trim() != "")
                {
                    cjoID = int.Parse(Request["cjoID"].ToString().Trim());

                    cjoIteM = new DataLib.BLL.CJOItem().GetListByCid(cjoID);
                    if (cjoIteM.islocal == false)
                    {
                        Response.Redirect("CJONonlocalRDLC.aspx?cjoid="+cjoID.ToString());
                        return;
                    }
                    BindData(cjoIteM);
                    //GetReportMultipleDataSourceFile
                }
                else
                {
                    if (Request["outid"] != null)
                    {
                        BindOutID(Request["outid"]);
                    }
                }

            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="cjoIteM"></param>
        protected void BindData(DataLib.Model.CJOItem cjoIteM)
        {
            List<ReportParameter> parameterList = new List<ReportParameter>();

            DataLib.Model.LoadNum loadModel = new DataLib.BLL.LoadNum().GetModel(cjoIteM.loadnumid);
            DataLib.Model.CumtomerAddress addrModel = new DataLib.BLL.CumtomerAddress().GetModel(int.Parse(cjoIteM.Addressid.ToString()));
            DataLib.Model.Cumtomer cumModel = new DataLib.BLL.Cumtomer().GetModel(cjoIteM.customerid);
            DataLib.Model.VehicleRequire vehicleM = new DataLib.BLL.VehicleRequire().GetModel(cjoIteM.vehichlerequireid);
            DataLib.Model.Cjo cModel = new DataLib.BLL.Cjo().GetModel(cjoIteM.cjoid);
            string cumname = "";
            if (loadModel.cjoname != null && loadModel.cjoname.Trim() != "")
            {
                cumname = loadModel.cjoname;
            }
            else
            {
                cumname = loadModel.cumname;
            }

            //报表信息为主参数
            parameterList.Add(new ReportParameter("CreateTime", cjoIteM.createtime.ToString()));
            parameterList.Add(new ReportParameter("LoadNum", cjoIteM.loadnumno));
            parameterList.Add(new ReportParameter("CollectDate", cjoIteM.collectdate.ToString()));
            parameterList.Add(new ReportParameter("CustomerName", cumname));
            parameterList.Add(new ReportParameter("CustomerCode", cjoIteM.pronum));
            parameterList.Add(new ReportParameter("WorkerCount", cjoIteM.workercount.ToString()));
            parameterList.Add(new ReportParameter("Vehicle", cjoIteM.other.ToString()));
            parameterList.Add(new ReportParameter("GoodsCount", cjoIteM.numberestimate.ToString()));
            parameterList.Add(new ReportParameter("SamplingRequire", cjoIteM.samplingrequire.ToString()));
            parameterList.Add(new ReportParameter("parMemo", cjoIteM.cjoremark));
            parameterList.Add(new ReportParameter("reportIstyz", cjoIteM.istyz));

            string[] petiReq = cjoIteM.peculiarrequire.Split(';');
            string peticularRequire = "";
            for (int i = 0; i < petiReq.Length; i++)
            {
                if (petiReq[i] != null && petiReq[i].Trim() != "")
                {
                    peticularRequire += petiReq[i] + "　";
                }
            }
            parameterList.Add(new ReportParameter("PeculiarRequire", peticularRequire));

            string[] tools = cjoIteM.tools.Split(';');
            string tool = "";
            for (int i = 0; i < tools.Length; i++)
            {
                if (tools[i] != null && tools[i].Trim() != "")
                {
                    tool += tools[i] + "\n";
                }
            }
            parameterList.Add(new ReportParameter("Tools", tool));

            string[] maternRequire = cjoIteM.maternrequire.ToString().Split(';');
            string matern = "";
            for (int i = 0; i < maternRequire.Length; i++)
            {
                if (maternRequire[i] != null && maternRequire[i].Trim() != "")
                {
                    matern += maternRequire[i] + "　";
                }
            }
            parameterList.Add(new ReportParameter("MaternRequire", matern));
            parameterList.Add(new ReportParameter("BCity", cjoIteM.belongArea));
            parameterList.Add(new ReportParameter("WasteDisposePlace", cjoIteM.Graphicy));
            parameterList.Add(new ReportParameter("GoodsWeight",cjoIteM.predictweight.ToString()));
            parameterList.Add(new ReportParameter("Description", cjoIteM.goodsdesc));

            string[] newWaste = loadModel.RubbishType.Split(';');
            string wasteType = "";
            for (int i = 0; i < newWaste.Length; i++)
            {
                if (newWaste[i] != null && newWaste[i].Trim() != "")
                {
                    wasteType += newWaste[i] + "\n";
                }
            }
            parameterList.Add(new ReportParameter("WasteName", wasteType));
            parameterList.Add(new ReportParameter("ContactPerson", cModel.cjolinkman));
            parameterList.Add(new ReportParameter("ContactPhone", cModel.cjophone));
            parameterList.Add(new ReportParameter("CustomerAddress", cModel.cjoadd));
            parameterList.Add(new ReportParameter("OtherRequire", vehicleM.otherrequire.ToString()));
            parameterList.Add(new ReportParameter("PayType", vehicleM.clearingform));
            parameterList.Add(new ReportParameter("OtherCost", vehicleM.othercost.ToString()));
            parameterList.Add(new ReportParameter("EditionDate", DateTime.Now.ToString()));
            parameterList.Add(new ReportParameter("othervrequire",vehicleM.othervrequire));
            string plateNumInfo = "";
            plateNumInfo = GetPlateNo((int)cjoIteM.cjoid, 1);


            parameterList.Add(new ReportParameter("PlateNumber", plateNumInfo));
           // DataLib.Accounts.Bus.User user = new DataLib.Accounts.Bus.User(cjoIteM.cjoshowmanusername);
            parameterList.Add(new ReportParameter("PhoneNum",cjoIteM.sealmanphone));
            parameterList.Add(new ReportParameter("SalesMan",cjoIteM.cjoshowmantruename));

            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.SetParameters(parameterList);
            ReportViewer1.LocalReport.Refresh();
        }

        public void BindOutID(object oid)
        {
            int outid = int.Parse(oid.ToString());
            DataLib.Model.OutStorage oModel = new DataLib.BLL.OutStorage().GetModel(outid);
            DateTime dateNotice = (DateTime)oModel.outStorageDate;
            string graphic = oModel.graphicCity;
            string cumname = "";
            string cumcode = "";
            //if (oModel.inDepart == oModel.belogarea && oModel.OutDepart != oModel.graphicCity && oModel.StorageType != "原料库")
            //{
            //    cumname = oModel.OutDepart + oModel.graphicCity + "库";
            //}
            //else if (oModel.StorageType != "原料库")
            //{
            //    cumname = oModel.inDepart + "伟翔";
            //}
            //else
            //{
            //    DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetLoadID(oModel.VLoadNum);
            //    if (lmodel != null)
            //    {
                    
            //            cumname = new DataLib.BLL.Cumtomer().GetModel((int)lmodel.customerid).companyname;

            //            cumcode = new DataLib.BLL.Cumtomer().GetModel((int)lmodel.customerid).customercode;
            //    }
            //}
            if (oModel.VLoadNum.Substring(0, 2) == "86")
            {
                DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetLoadID(oModel.VLoadNum);
                cumname = lmodel.cumname;
                cumcode = lmodel.cumcode;
            }
            else
            {            
                cumname = oModel.inDepart + "伟翔";
                cumcode = cumname; ;
            }
            string LoadNo = oModel.VLoadNum;
            string plateno = GetPlateNo((int)oModel.id,0);
            List<ReportParameter> parameterList = new List<ReportParameter>();
            parameterList.Add(new ReportParameter("PlateNumber", plateno));
            parameterList.Add(new ReportParameter("CreateTime", dateNotice.ToString()));
            parameterList.Add(new ReportParameter("LoadNum", LoadNo));
            parameterList.Add(new ReportParameter("CustomerName", cumname));
            parameterList.Add(new ReportParameter("CustomerCode", cumcode));
            parameterList.Add(new ReportParameter("WasteDisposePlace", graphic));
            parameterList.Add(new ReportParameter("BCity", oModel.belogarea));

            ReportViewer1.Visible = true;
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.SetParameters(parameterList);
            ReportViewer1.LocalReport.Refresh();
        }

        /// <summary>
        /// 车辆信息
        /// </summary>
        /// <param name="srcid"></param>
        /// <param name="flag">1是cjo，否则是out</param>
        /// <returns></returns>
        public string GetPlateNo(int srcid,int flag)
        {
            string plateNumInfo = "";
            //添加车牌和运输公司信息
            DataLib.Model.VehicleArrange vehArrModel = new DataLib.Model.VehicleArrange();
            if (flag == 1)
            {
                vehArrModel = new DataLib.BLL.VehicleArrange().GetModelByCjoid(srcid);
            }
            else
            {
                vehArrModel = new DataLib.BLL.VehicleArrange().GetModelByOutid(srcid);
            }
            DataLib.Model.PlateNumber plateNuM = new DataLib.Model.PlateNumber();
            if (vehArrModel != null)
            {
                string[] brr = vehArrModel.platenumberid.Split(',');
                for (int i = 0; i < brr.Length; i++)
                {
                    if (brr[i] != "")
                    {
                        plateNuM = new DataLib.BLL.PlateNumber().GetModel(int.Parse(brr[i]));
                        plateNumInfo += new DataLib.BLL.TransportCompany().GetModel(plateNuM.transportIid).companyname + "--" + plateNuM.maxton + "--" + plateNuM.plateno;
                        if (vehArrModel.sealno.IndexOf(brr[i]) >= 0)
                        {
                            string[] arrSealNo = vehArrModel.sealno.Split('@');
                            if (arrSealNo.Length > 0)
                            {
                                for (int l = 0; l < arrSealNo.Length; l++)
                                {
                                    if (arrSealNo[l].IndexOf(brr[i]) < 0)
                                        continue;
                                    else
                                    {
                                        string[] arrPlateID = arrSealNo[l].Split(':');
                                        if (arrPlateID.Length > 1)
                                        {

                                            plateNumInfo += "-封签（" + arrPlateID[1] + "）";
                                            break;

                                        }
                                    }
                                }
                            }
                        }
                        plateNumInfo += "\n";
                    }
                }
            }
            return plateNumInfo;
        }
    }
}